<?php
$lightheader = TRUE;
error_reporting(E_ALL);
include('include/config.inc.php');
	
$sql  = "SELECT * FROM cdr ORDER BY CallId DESC LIMIT " . $config['TOPX'] . "";

if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{

	include('include/header.inc.php');
	echo '<br \>';
	//echo '<br \>';

	echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
	echo ' <tr height="36">';
	echo '  <td width="26" background="images/tl-gry.gif">&nbsp;</td>';
	echo '  <td background="images/t-gry.gif" colspan="2"><b><font color=white>Ergebnis</font><b></td>';
	echo '  <td width="10" background="images/tr-gry.gif"></td>';
	echo ' </tr>';
	echo '</table>';
		
	echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
	echo ' <tr height="10">';
	echo '  <td width="10" background="images/mt-l.gif">&nbsp;</td>';
	echo '  <td background="images/mt-c.gif" colspan="2">&nbsp;</td>';
	echo '  <td width="10" background="images/mt-r.gif">&nbsp;</td>';
	echo ' </tr>';
	echo '</table>';
		
	echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
	echo ' <tr>';
	echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
	echo '  <td align="left"></td>';
	echo '  <td align="left"><b>' . $lang['originatingnumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['originatingname'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['callednumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['calledname'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['destinationnumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['destinationname'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['startime'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['state'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['disconnectreason'] . '</b></td>';
	echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
	echo ' </tr>';

	$callduration_fromnumber = 0;
	$callduration_fromname = 0;
	$callduration_callednumber = 0;
	$callduration_calledname = 0;
	$callduration_destnumber = 0;
	$callduration_destname = 0;

	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
			
		// TOTAL CALL DURATION
		//if ( isset($_POST["fromnumber"]) && $_POST["fromnumber"]!="0" )
		//{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_fromnumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		//}
		//	if ( isset($_POST["fromname"]) && $_POST["fromname"]!="0" )
		//{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_fromname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		//}

		//if ( isset($_POST["callednumber"]) && $_POST["callednumber"]!="0" )
		//{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_callednumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		//}
		//	if ( isset($_POST["calledname"]) && $_POST["calledname"]!="0" )
		//{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_calledname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		//}
			if ( isset($_POST["destnumber"]) && $_POST["destnumber"]!="0" )
		{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_destnumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		}
		
		if ( isset($_POST["destname"]) && $_POST["destname"]!="0" )
		{
			if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
			{
				$callduration_destname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
			}
		}
		# Rewrite Date for Database 
		$Newdate = explode("-", $row['StartDate']);
		#echo '1' . $row['StartDate'] . '';
		$Newday = explode(" ", $Newdate[2]); 
		$Tag = $Newday[0];
		#echo "Tag: $Tag";
		$Monat = $Newdate[1];
		#echo "Monat: $Monat";
		$Jahr = $Newdate[0];
		#echo "Jahr: $Jahr";
		$Zeit = $Newday[1];
		#echo "Zeit: $Zeit";
		$Insertdate = "$Tag.$Monat.$Jahr $Zeit";
		
		
		echo '<tr id="' . $row['CallId'] . '" name="' . $row['CallId'] . '" onMouseOver="ChangeBackground(\'' . $row['CallId'] . '\')" onMouseOut="ResetBackground(\'' . $row['CallId'] . '\')">';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td align="right"><a style="cursor: pointer;" onclick="javascript:window.open(\'info.php?callid=' . $row['CallId'] . '\',\'Call_Info\',\'status=0,toolbar=0,location=0,menubar=0,scrollbars=0,width=580,height=620\');"><img src="images/info.gif" border="0" alt="' . $lang['morecallinfo'] . '" title="' . $lang['morecallinfo'] . '"></td>';
		if ( trim($row['OriginationNumber'])=="" )
		{
			echo ' <td><img src="images/nophone.gif" border="0" alt="No Number" title="No Number">&nbsp;XXX</td>';
		}
		else
		{
			echo ' <td><a href="callto://' . $row['OriginationNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['OriginationNumber'] . '</td>';
		}
		echo ' <td>&nbsp;' . $row['OriginationName'] . '</td>';
		echo ' <td><a href="callto://' . $row['CalledNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['CalledNumber'] . '</td>';
		echo ' <td>&nbsp;' . $row['CalledName'] . '</td>';
		echo ' <td><a href="callto://' . $row['CalledNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['DestinationNumber'] . '</td>';
		echo ' <td>&nbsp;' . $row['DestinationName'] . '</td>';
		echo " <td>&nbsp; $Insertdate</td>";
		echo ' <td>&nbsp;' . $row['State'] . '</td>';
		echo ' <td>&nbsp;' . $row['DisconnectReason'] . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';
	}
	echo '</table>';
echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
echo '<tr height="10">';
echo '  <td width="10" background="images/b-l.gif">&nbsp;</td>';
echo '  <td background="images/b-c.gif" colspan="2">&nbsp;</td>';
echo '  <td width="10" background="images/b-r.gif">&nbsp;</td>';
echo ' </tr>';
echo '</table><br>';
		

	}

include('include/footer.inc.php');

?>